package de.gympu.gympulan.dbmysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import de.gympu.gympulan.core.plugins.gpldatabaseplugin.GPLDatabase;
import de.gympu.gympulan.core.plugins.gpldatabaseplugin.GPLDatabaseException;
import de.gympu.gympulan.core.plugins.gpldatabaseplugin.GPLQuery;

public class MySQL implements GPLDatabase {
	/**
	 * Verbindung
	 */
	private Connection con;
	
	/**
	 * MySQL Statement
	 */
	private  Statement mysql;
	
	/**
	 * Baut die Verbidnug zur Datenbank auf.
	 * @throws GPLDatabaseException Wenn die Datenbank nicht gefunden wird
	 */
	public void connect(String serverName, int port, String username, String password, String database) throws GPLDatabaseException {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		}
		catch(ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			con = DriverManager.getConnection("jdbc:mysql://" + serverName + ":" + port + "/" + database,username,password);
			mysql = con.createStatement();
		}
		catch(Exception e) {
			throw new GPLDatabaseException(e.getMessage());
		}
	}
	
	public ResultSet getResultSet(String query) throws SQLException {
			return mysql.executeQuery(query);
	}

	public Statement getStatement() {
		return mysql;
	}
	
	public void disconnect() {
		try {
			con.close();
		} catch (SQLException e) {}
	}
	
	/**
	 * Sendet einen UPDATE Query an die Datenbank
	 * @param query Update Query
	 */
	public void update(String query) {
		try {
			mysql.executeUpdate(query);
		}
		catch(SQLException e) {
			System.out.println(e);
		}
	}
	
	public int getLastID() {
		try {
			ResultSet result = getStatement().getGeneratedKeys();
			if(result.next()) return result.getInt(1);
			else return 0;
		}
		catch(SQLException e) {
			System.out.println(e);
		}
		return 0;
	}
	
	public GPLQuery query(String query) {
		return new SQLSelect(query);
	}

	
	private class SQLSelect implements GPLQuery {
		private ResultSet result;
		
		public SQLSelect(String query) {
			try {
				this.result = getResultSet(query);
			}
			catch(SQLException e) {
				throw new GPLDatabaseException("Fehler in der Datenbank: " + e.getMessage());
			}
		}
		
		public boolean hasNext() {
			try {
				return result.next();
			}
			catch(Throwable e) {
				// e.printStackTrace();
			}
			return false;
		}
		
		public String getString(String index) {
			try {
				return result.getString(index);
			}
			catch(SQLException e) {
				e.printStackTrace();
			}
			return null;
		}
		
		public int getInt(String index) {
			try {
				return result.getInt(index);
			}
			catch(SQLException e) {
				e.printStackTrace();
			}
			return 0;
		}
		
		public double getDouble(String index) {
			try {
				return result.getDouble(index);
			}
			catch(SQLException e) {
				e.printStackTrace();
			}
			return 0.0;
		}
		
		public boolean getBoolean(String index) {
			try {
				return (result.getInt(index) > 0);
			}
			catch(SQLException e) {
				e.printStackTrace();
			}
			return false;
		}
		
		public long getLong(String index) {
			try {
				return result.getLong(index);
			}
			catch(SQLException e) {
				e.printStackTrace();
			}
			return 0;
		}
	}


	
	
}
